package arch.cpu;

import java.util.Observable;

import util.SysHelper;

public final class RegisterGroup extends Observable
{
	private static final int regArraySize = 16;
	public static final int PC = -1;
	public static final int ZF = -2;
	private int registerArray[] = new int[regArraySize];
	private int pc;
	private boolean zflag;
	private boolean isReg;

	public RegisterGroup()
	{
	}

	public int[] getRegs()
	{
		return this.registerArray;
	}

	public void reset()
	{
		registerArray = new int[regArraySize];
		isReg = false;
		setPC(0);
		zflag = false;
		notifyUI();
	}

	public void set(int index, int value)
	{
		if (PC == index)
		{
			pc = value;
		}
		else if (ZF == index)
		{
			zflag = (value == 0 ? false : true);
		}
		else
		{
			isReg = true;
			notifyUI();
			SysHelper.sleep();

			registerArray[index] = value;
			isReg = false;
		}
		notifyUI();
	}

	public int get(int index)
	{
		if (PC == index)
		{
			return pc;
		}

		if (ZF == index)
		{
			return zflag ? 1 : 0;
		}

		isReg = true;
		SysHelper.sleep();
		isReg = false;
		return registerArray[index];

	}

	public void notifyUI()
	{
		setChanged();
		notifyObservers(new Object[]
		{ isReg, registerArray, pc, zflag });
	}

	/**
	 * @param pc
	 *            the pc to set
	 */
	public void setPC(int pc)
	{
		this.pc = pc;
	}

	/**
	 * @return the pc
	 */
	public int getPC()
	{
		return pc;
	}

	public void setZF(boolean value)
	{
		this.zflag = value;
		notifyUI();
	}

	public boolean getZF()
	{
		return zflag;
	}
}